package com.wf.smartcomm.server.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Advertisement;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.AdService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-25 上午10:25:53 
 * 
 */
@Service
@Transactional
public class AdServiceImpl implements AdService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Advertisement ad) {
		ad.setObjectId(IdUtils.uuid());
		ad.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		ad.setUpdateAt(ad.getCreatedAt());
		factory.getCurrentSession().persist(ad);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByVillage(Village village, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Advertisement where village = :village");
		query.setParameter("village", village);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Advertisement where village =:village order by updateAt desc");
		query.setParameter("village", village);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	public Advertisement get(String id) {
		Query query = factory.getCurrentSession().createQuery("from Advertisement where objectId =:id");
		query.setParameter("id", id);
		return (Advertisement) query.uniqueResult();
	}

	@Override
	public void update(Advertisement ad) {
		ad.setUpdateAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().update(ad);
	}

	@Override
	public void delete(Advertisement ad) {
		factory.getCurrentSession().delete(ad);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void query(Village loginVillage, String key, Page page) {
		String hql = "select count(*) from Advertisement where village =:village and (subject like :key or content like :key)";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("village", loginVillage);
		query.setParameter("key", "%"+key+"%");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		hql = "from Advertisement where village =:village and (subject like :key or content like :key) order by updateAt desc";
		query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("village", loginVillage);
		query.setParameter("key", "%"+key+"%");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	public Advertisement getLatestAd(User user) {
		Query query = factory.getCurrentSession().createQuery("from Advertisement where village =:village order by createdAt desc").setFirstResult(0).setMaxResults(1);
		query.setParameter("village", user.getLoginVillage());
		List<Advertisement> advertisements = query.list();
		if (advertisements != null && advertisements.size() > 0) {
			return advertisements.get(0);
		}
		return null;
	}

}
